﻿using BLL;
using DAL;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    
    public partial class UserInfor : System.Web.UI.Page
    {
        public int Count;
        public int Count1 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {    
            if (!IsPostBack)
            {
               
                Label6.Text =1.ToString();
                UserNum();
                UserInfors(0,"");
                Zys();


            }
           
        }
       //始页
        void UserInfors(int ID,String UName)
        {
           
            GridView1.DataSource = UsersBLL.UserInforAll(ID,UName);
            GridView1.DataBind();
          
        }
        void UserNum()
        {
            int a  = UsersBLL.UserAll();
            Label1.Text = "用户数据有" + a + "条";
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }
        //用户的删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (UsersBLL.UserInfoDel(int.Parse(this.GridView1.Rows[e.RowIndex].Cells[0].Text)))
            {
                UserNum();
                Button1_Click(1,e);
              
                Response.Write("<script>alert('删除成功')</script>");
               
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //GridView1分页
        //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    GridView1.PageIndex = e.NewPageIndex;
        //    UserInfors("");
        //}

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            UserInfors(0, "");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            UserInfors(0, "");
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Models.UserInfor us = new Models.UserInfor(); 
            us.UsersID= int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            us.UName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            us.UsersName=((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        
            us.UsersAge =int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
            us.UsersSex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            us.UsersPostbox = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
            us.UsersPhone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
            us.Address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
            
            if (UsersBLL.UserInfoUpda(us))
            {
                Response.Write("<script>alert('修改成功')</script>");
                GridView1.EditIndex = -1;
                UserInfors(0, "");
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (UsersBLL.UserInforAll(0,TextBox1.Text).Count>0)
            {
                GridView1.DataSource = UsersBLL.UserInforAll(0,TextBox1.Text);
                GridView1.DataBind();
            }
            else
            {
                Response.Write("<script>alert('未找到该用户信息')</script>");
                UserInfors(0, "");
            }
        }
        //Sql语句分页
        //总页数
        public void Zys()
        {
            Label8.Text =Convert.ToString(UsersBLL.UserAll());

            Count = int.Parse(Label8.Text) % 5;
            if (Count == 0)
            {
                Label8.Text = (int.Parse(Label8.Text)/ 5).ToString();
            }
            else
            {
                Label8.Text = (int.Parse(Label8.Text) / 5+1).ToString();
            }
        }
        //首页
        protected void Button1_Click(object sender, EventArgs e)
        {
            UserInfors(0, "");
            Count1 = 0;
            Label6.Text =Convert.ToString(1);
        }
        //上一页
        protected void Button2_Click(object sender, EventArgs e)
        {

         
            if (int.Parse(Label6.Text)>1)
            {

                Count1 = int.Parse(Label6.Text)-1;
                Label6.Text = Count1.ToString();
               
                UserInfors((Count1-1) * 5, "");
            }
            else
            {
                Response.Write("<script>alert('翻页失败')</script>");
            }
        }
        //下一页
        protected void Button5_Click(object sender, EventArgs e)
        {
          
            if (int.Parse(Label6.Text)<=int.Parse(Label8.Text)-1)
            {
                Count1 = int.Parse(Label6.Text);
                UserInfors(Count1 * 5, "");
             
                Label6.Text = (Count1+1).ToString();




       

            }
            else
            {
                Response.Write("<script>alert('翻页失败')</script>");
            }



        }
        //尾页

        protected void Button4_Click(object sender, EventArgs e)
        {
            UserInfors((int.Parse(Label8.Text) - 1)*5, "");
            Label6.Text = Label8.Text;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (int.Parse(TextBox2.Text.Trim())>1&& int.Parse(TextBox2.Text.Trim())<=int.Parse(Label8.Text))
            {
                UserInfors((int.Parse(TextBox2.Text.Trim())-1)*5, "");
                Label6.Text = TextBox2.Text;
            }
            else
            {
                Response.Write("<script>alert('输入的页数无效')</script>");
            }
        }
    }
}